/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.tools.remoting {
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.data.value.MLBooleanValue;
	import com.googlecode.milib.tools.MLEventsReflector;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;

	/**
	 * @author Marek Brun
	 */
	public class AbstractService extends MLBroadcastClass implements IMLBroadcasterListener {
		
		public const event_StartLoading:String='StartLoading';
		
		public const event_FinishLoading:String='FinishLoading';
		
		protected var connection:NetConnection;
		private var isLoading:MLBooleanValue;
		private var gatewayURL:String;
		protected var className:String;
		private var notStartedResponders:MLEventsReflector;
		private var runningResponders:MLEventsReflector;
		
		public function AbstractService(gatewayURL:String, className:String) {
			this.gatewayURL=gatewayURL;
			this.className=className;
			
			connection=new NetConnection();
			//connection.objectEncoding=ObjectEncoding.AMF0;
			connection.addEventListener(NetStatusEvent.NET_STATUS, onConnectionNetStatusEventNETSTATUS);
			try{
				connection.connect(gatewayURL);
			}catch(e:Error){
				dbg.log('NetConnection.connect Error, message:'+e.message);
			}
			
			notStartedResponders=new MLEventsReflector(true);
			notStartedResponders.addListener(this);
			
			runningResponders=new MLEventsReflector(true);
			runningResponders.addListener(this);
			
			isLoading=new MLBooleanValue(false);
			isLoading.addListener(this);
			
		}
		
		/*abstract*/ protected function createResponderInstance(methodName:String, args:Array):MLResponderAction {
			return new MLResponderAction(connection, className, methodName, args);
		}
		
		protected function createResponder(methodName:String, args:Array):MLResponderAction {
			var responder:MLResponderAction=createResponderInstance(methodName, args);
			if(!responder){
				dbg.log('Warning: fail to create responder for method:'+methodName+' with arguments:'+dbg.link(args));
				return null;
			}
			notStartedResponders.addObject(responder);
			return responder;
		}
		
//********************************************************************************************
//		events for AbstractService
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			//super.onEvent(ev);
			switch(ev.hero){
				case notStartedResponders:
					if(ev.event!=ev.hero.event_EventReflection){ break; }
					var hero:MLResponderAction=MLResponderAction(ev.data.hero);
					switch(ev.data.event){
						case hero.event_ActionStart:
							notStartedResponders.removeObject(hero);
							runningResponders.addObject(hero);
							isLoading.v=true;
						break;
					}
				break;
				case runningResponders:
					if(ev.event!=ev.hero.event_EventReflection){ break; }
					var hero2:MLResponderAction=MLResponderAction(ev.data.hero);
					switch(ev.data.event){
						case hero2.event_ActionFinish:
							runningResponders.removeObject(hero2);
							if(!runningResponders.getAddedObjects().length){
								isLoading.v=false;
							}
						break;
					}
				break;
				case isLoading:
					switch(ev.event){
						case isLoading.event_Changed:
							if(isLoading.v){
								broadcastEvent(event_StartLoading);
							}else{
								broadcastEvent(event_FinishLoading);
							}
						break;
					}
				break;
			}
		}
		
		protected function onConnectionNetStatusEventNETSTATUS(event:NetStatusEvent):void {
			dbg.log('net status event type:'+event.type+' info:'+dbg.link(event.info, true));
		}
		
	}
}
